﻿using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using my_console5;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

// M01();
// M02();
// M03();
// M04();
// M05();
// M06();
M07();

Console.ReadLine();

static async void M01()
{
    // var json = File.ReadAllText("param.json");
    // var result = await ElasticHelper.SearchAsync("archive_active", json);
    // var obj = AggOutput2.Parse(result.SelectToken("aggregations"));
    // System.Console.WriteLine(JsonConvert.SerializeObject(obj));
    var stat = new StatPara
    {
        Filter = new StatFilter
        {
            Fields = [
                // new() { Key = "Hospital", Label = "医院", Value = ["Haidian", "400823815", "100A1007", "ADWC88888888", "csjg003", "csjg004", "csjg2901", "400880246_1", "400880246", "400755584", "BJTRYY"] },
                new() { Key = "HospitalTag", Label = "机构标签", Value = ["MutualRecognitionProvider", "MutualRecognitionOnline"] }
            ],
            TimeRange = new StatTimeRange
            {
                TimeType = "ApproveTime",
                Value = [DateTime.Now.AddYears(-1).Date, DateTime.Now.Date]
            }
        },
        Groups = [
            new StatField { Key = "Hospital", Label = "医院" },
            new StatField { Key = "PatientType", Label = "患者来源" },
        ]
    };
    // var query = AggInput.Parse(stat);
    // File.WriteAllText("query.json", query.ToString());

    var result = await AggInput.SearchAsync("archive_active", stat);
    File.WriteAllText("result.json", result.ToString());
}

static void M02()
{
    System.Console.WriteLine(Convert.ToBoolean("true"));
}

static void M03()
{
    var www = Guid.NewGuid().ToString();
    System.Console.WriteLine("Hello, World!" + Guid.NewGuid());
    System.Console.WriteLine("Hello, World!" + www);
}

static void M04()
{
    var html = File.ReadAllText("01.html");
    var match = Regex.Match(html, "<span id=\"GLCD_year\" [^>]*>(.*?)</span>");
    if (match.Success)
    {
        var content = match.Groups[1].Value;
        System.Console.WriteLine("Matched content: " + content);
    }
}

static void M05()
{
    var data = "jXRDV4tCRBrbdAPil/4TRV7Pcsq7khrd/0/fK4CckFB+OQgyT+rfxfSODluIzrxktVoyBjvJihzqryeEW5MfxDzM+LBKJZVWn5Inf2kxFBOiUx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7a7a/84Pe2dVBHFa6uRW8o1wiR5wkaHQ/nvVyT7FUU6OkvqxPeVLLAiptLA6th4v5ntj6TgTtaF0tcRToOM3A/mHznjbXZGEiF5TefHzojhXrU+y+vT3ceFsAWkH4/tFqO0hCOjnp9RFsgfwePysWxZqxXXn4pvzqi9GzYd2D83SWPCfWPQ81WzOk27QEiZ2AAoHRNGAlvkAyZ9NPCLtDcqF2nmmK3Jzhu7IVGaHy45IU8jylW+zJmSGDkR51gZlHPqRPYNLGQ5FZaUpersSzv2G9mJCAuy0LJQs/A09KLXgbDccu05wDLXV5jFfOzSI5xA/eEvoBPMuv/D+jWzOdLQkFC9SKI+nQPXPnn3qDZtu2X0anbq260LhhqcvtIyEE95OZCHwcUHH+7WbYoI65F64ucnFu4QTDQzuWcwN4kIXePlx5u62sh7MVTF8Ms4rrKwUOvu9XOphkzwhcLc0wn5sLPhtVNyx2vs3RKX5YTpWysLtNF8y3ADcXGFR4mcCUt9GkFLcqYJKjwyd2EKX8zd13P46AjiK/J33a3ev1SmlS9Nwlieutp1EY3n1LM=";
    var text = AesDecrypt2(data);
    // var data = "8FvHJcQmVojAIU61SNaS1ermHN2UVWknueRHFSNf2q5EbxNNmznoTYpRu7ySc/8CuU+QGZ9UIBMCyTuFafY3PuszEokEKc8M1Qfv/+o15h5bIU8LXfwRKOCm3JYzZtTOvJVU0hk/USvtDgraToszFl2hQZjZN5gGH1af0X8vopo=";
    // var text = AesDecrypt2(data, "0bcbe9d6e6124cf2aef2856a540f1326");
    text = text.Trim('\0');
    Console.WriteLine(JObject.Parse(text).ToString());
}

/// <summary>
/// AES解密京东订单接口返回的密文
/// </summary>
/// <param name="cipherText">加密的密文（Base64编码）</param>
/// <param name="key">解密密钥</param>
/// <param name="iv">初始向量（CBC模式需要）</param>
/// <returns>解密后的明文</returns>
static string AesDecrypt2(string cipherText, string appSecret = null){
    appSecret ??= "557f163386d84899826e9b195ad2fc59";
    var key = appSecret[..16];
    var iv = appSecret[16..];
    try
    {
        // 将密文从Base64转换为字节数组
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        // 转换密钥为字节数组（根据实际情况选择编码，如UTF8、ASCII等）
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);
        // 转换初始向量为字节数组
        byte[] ivBytes = string.IsNullOrEmpty(iv) ? null : Encoding.UTF8.GetBytes(iv);
        using Aes aesAlg = Aes.Create();
        // 设置加密模式（根据京东接口文档配置）
        aesAlg.Mode = CipherMode.CBC; // 常见模式，可能需要根据实际情况修改
        // 设置填充模式（根据京东接口文档配置）
        aesAlg.Padding = PaddingMode.None;
        // 设置密钥和初始向量
        aesAlg.Key = keyBytes;
        if (ivBytes != null) aesAlg.IV = ivBytes;
        // 创建解密器
        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
        // 解密操作
        using MemoryStream msDecrypt = new(cipherBytes);
        using CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read);
        using StreamReader srDecrypt = new(csDecrypt);
        // 读取解密后的明文
        return srDecrypt.ReadToEnd();
    }
    catch (Exception ex)
    {
        Console.WriteLine($"解密失败: {ex.Message}");
        return null;
    }
}


static void M06()
{
    using var http = new HttpClient();
    var url = "https://00pingxuan.00pingxuan.top/huodong2/index/pltp.html";
    var json = File.ReadAllText("up.json");
    http.DefaultRequestHeaders.Add("Host", "00pingxuan.00pingxuan.top");
    http.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
    http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 NetType/WIFI WindowsWechat(0x63090a13) UnifiedPCWindowsWechat(0xf2541015) XWEB/16389 Flue");
    http.DefaultRequestHeaders.Add("Accept", "application/json, text/plain, */*");
    // http.DefaultRequestHeaders.Add("Content-Type", "application/json;charset=UTF-8");
    http.DefaultRequestHeaders.Add("Origin", "https://00pingxuan.00pingxuan.top");
    http.DefaultRequestHeaders.Add("Sec-Fetch-Site", "same-origin");
    http.DefaultRequestHeaders.Add("Sec-Fetch-Mode", "cors");
    http.DefaultRequestHeaders.Add("Sec-Fetch-Dest", "empty");
    http.DefaultRequestHeaders.Add("Referer", "https://00pingxuan.00pingxuan.top/details/49455971?token=q2MBgv2&code=031vu8ml2nSaag4zyHkl2UJjaL1vu8mY&state=STATE");
    http.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
    http.DefaultRequestHeaders.Add("Accept-Language", "zh-CN,zh;q=0.9");
    http.DefaultRequestHeaders.Add("Cookie", "PHPSESSID=qd048171aimbm4294p6hrfl1g0; gdxidpyhxdE=59vdPzYJpEYdyh0%5COt8%2B1Nunek%2Fzd4eMNZGGED00M73Y2QimM%5CZAwmflKsAZAcruBCsJjvCDehoWZnUn1AWqcarLNgapqLm71tvm4tAKdyhZDJPx3DBQT349iIxvKPw3x4gMvoCcBTxUB3nbA%2BK7maUqyRAK%2BDxaO0vyyn82zw6%5CZo%2Bk%3A1756191348561");
    var content = new StringContent(json, Encoding.UTF8, "application/json");
    http.PostAsync(url, content).ContinueWith(t =>
    {
        var response = t.Result;
        var result = response.Content.ReadAsStringAsync().Result;
        Console.WriteLine(result);
    }).Wait();

}

static void M07()
{
    var json = File.ReadAllText("up.json");
    // var obj = JObject.Parse(json);
    // System.Console.WriteLine(obj.ToString());
    var aaa = JsonConvert.DeserializeObject<StatisticOrder>(json);
    System.Console.WriteLine(JsonConvert.SerializeObject(aaa));
}


